package com.fj.netty.chat.handler;

import com.fj.netty.chat.message.ChatRequestMessage;
import com.fj.netty.chat.server.service.ServiceFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author fj
 * @date 2024/1/12 13:43
 */
@Slf4j
public class ChatRequestMessageHandler extends SimpleChannelInboundHandler<ChatRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ChatRequestMessage msg) throws Exception {
        String msgTo = msg.getTo();
        Channel channel = ServiceFactory.getSession().getChannel(msgTo);
        if (channel == null){
            log.debug("目标用户：{}未登录",msgTo);
            return;
        }
        channel.writeAndFlush(msg);
    }
}
